package com.yyj.controller;

import com.github.pagehelper.PageInfo;
import com.yyj.entity.*;
import com.yyj.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*跨域*/
@CrossOrigin
@RestController
@RequestMapping()
public class UserController {
    @Autowired
    UserService userService;

    //通过查询浏览宠物狗的信息
    @GetMapping("/user/searchDog")
    public Result getDog(PetParams petParams) {
        List<Pet> list = userService.getDog(petParams);
        return Result.success(list);
    }

    //用户登录
    @PostMapping("/user/login")
    public Result Login(@RequestBody User user) {
        User loginUser = userService.login(user);
        return Result.success(loginUser);
    }

    @PostMapping("/user/register")
    public Result Register(@RequestBody User user) {
        userService.add(user);
        return Result.success();

    }
    //用户添加动物以及修改
    @PostMapping("/user")
    public Result insertDog(@RequestBody Pet pet) {
        if (pet.getPetId() == null) {
            //添加
            userService.insertDog(pet);
            return Result.success();
        } else {
            //修改
            userService.update(pet);
            return Result.success();
        }
    }

    //用户删除发布的宠物的方法
    @DeleteMapping("/user/{petId}")
    public Result deletePet(@PathVariable Integer petId) {
        userService.delete(petId);
        return Result.success();
    }

    //用户更改自己的照片
    @PostMapping("/user/updateInfo")
    public Result updateInfo(@RequestBody User user) {
        userService.updateImg(user);
        return Result.success();
    }

    //用户更改自己的头像
    @PostMapping("/user/updateImg")
    public Result updateImg(@RequestBody User user) {
        userService.updateImg(user);
        return Result.success();
    }



    //用户根据id查询自己的信息
    @GetMapping("/user/getInfo/{userId}")
    public Result getInfo(@PathVariable Integer userId) {
        User myInfo = userService.findById(userId);
        return Result.success(myInfo);
    }
    @GetMapping("/user/getMyAdopt/{newOwner}")
    public Result getMyAdopt(@PathVariable Integer newOwner) {
        List<Pet> list = userService.findByNewOwner(newOwner);
        return Result.success(list);
    }
    @PostMapping("/people/userSearch")
    public Result insertUser(@RequestBody User user) {

        if (user.getUserId() == null) {
            userService.insertUser(user);
        } else {
            //修改
            userService.updateUser(user);
        }
        return Result.success();
    }


    @GetMapping("/people/search")
    public Result getBySearch(Params params) {
        PageInfo<User> info = userService.getBySearch(params);
        return Result.success(info);
    }

    @DeleteMapping("/people/{userId}")
    public Result deleteUser(@PathVariable Integer userId){
        userService.deleteUser(userId);
        return Result.success();
    }




}
